//
//  ChooseWalletIconViewController.m
//  Wallets Manager
//
//  Created by Tung Nguyen on 10/1/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#define kImageRowHeight 70
#import "ChooseWalletIconViewController.h"

@interface ChooseWalletIconViewController ()
@end

@implementation ChooseWalletIconViewController
@synthesize fromView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 320, 380) style:UITableViewStylePlain];
    imageTableView.delegate = self;
    imageTableView.dataSource = self;
    imageTableView.tableFooterView = nil;
    imageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    imageTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:imageTableView];
    //=======================Load Image Names ======================//
    imageNames = [[NSMutableArray alloc]init];
    for (int i = 0; i<20; i++) {
        [imageNames addObject:[NSString stringWithFormat:@"icon_%d.png",i+1]];
    }
    // Do any additional setup after loading the view from its nib.
 
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - IBAction
- (IBAction)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) getImageOnSuccess :(id) sender {
    UIButton *tempButton = (UIButton *) sender ;
    NSString *imageName = [NSString stringWithFormat:@"icon_%d.png",tempButton.tag];

    NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:imageName,@"key",nil];
    if ( [fromView isEqualToString:@"AddWalletView"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"getImageOnSuccess" object:tempDict];
    } else {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"getPlanImageOnSuccess" object:tempDict];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [imageNames count]/4;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kImageRowHeight;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIndifier = @"cell";
    UITableViewCell *cell = nil;
    
    UIButton *firstImageButton;
    UIButton *secondImageButton;
    UIButton *thirdImageButton;
    UIButton *fourthImageButton;
    
    if( cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        firstImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        firstImageButton.frame = CGRectMake(10, 10, 60, 60);
        firstImageButton.tag = (indexPath.row *4)+1;
        [cell addSubview:firstImageButton];
        
        secondImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        secondImageButton.frame = CGRectMake(90, 10, 60, 60);
        secondImageButton.tag = (indexPath.row *4)+2;
        [cell addSubview:secondImageButton];
        
        thirdImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        thirdImageButton.frame = CGRectMake(170, 10, 60, 60);
        thirdImageButton.tag = (indexPath.row *4)+3;
        [cell addSubview:thirdImageButton];
        
        fourthImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        fourthImageButton.frame = CGRectMake(250, 10, 60, 60);
        fourthImageButton.tag = (indexPath.row *4)+4;
        [cell addSubview:fourthImageButton];
    }
    
    firstImageButton = (UIButton *)[cell viewWithTag:firstImageButton.tag];
    [firstImageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon_%d.png",firstImageButton.tag]] forState:UIControlStateNormal];
    [firstImageButton addTarget:self action:@selector(getImageOnSuccess :) forControlEvents:UIControlEventTouchUpInside];
    
    secondImageButton = (UIButton *)[cell viewWithTag:secondImageButton.tag];
    [secondImageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon_%d.png",secondImageButton.tag]] forState:UIControlStateNormal];
    [secondImageButton addTarget:self action:@selector(getImageOnSuccess :) forControlEvents:UIControlEventTouchUpInside];
    
    thirdImageButton = (UIButton *)[cell viewWithTag:thirdImageButton.tag];
    [thirdImageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon_%d.png",thirdImageButton.tag]] forState:UIControlStateNormal];
    [thirdImageButton addTarget:self action:@selector(getImageOnSuccess :) forControlEvents:UIControlEventTouchUpInside];
    
    fourthImageButton = (UIButton *)[cell viewWithTag:fourthImageButton.tag];
    [fourthImageButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon_%d.png",fourthImageButton.tag]] forState:UIControlStateNormal];
    [fourthImageButton addTarget:self action:@selector(getImageOnSuccess :) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
@end
